package pkg.chemdrm;

import java.util.ArrayList;
import java.util.LinkedList;

import org.anddev.andengine.engine.camera.hud.HUD;

public class InventoryHandler {
	
	ResourceHandler mResourceHandler;
	Substance mActiveSubstance;
	// figure out a way to store known substances (like ones we have discovered)
	// HashMap<Substance> or LinkedList<Substance> or something...
	
	// figure out a way to hold the mixing list
	// LinkedList<Substance> or whatever...
	ArrayList<Substance> mMixingList = new ArrayList<Substance>();
	
	ElementMoleculeDB mElementDB = new ElementMoleculeDB(); // this is all of our elements.

	ArrayList<InventoryObject> mInventoryObjects;
	
	HUD mHud;
	
	public InventoryHandler(HUD pHud, ResourceHandler pResourceHandler)
	{
		mHud = pHud;
		mInventoryObjects = new ArrayList<InventoryObject>();
		
		mResourceHandler = pResourceHandler;
		
		addSubstanceToInventory(mElementDB.getSubstance("Hydrogen"));
		addSubstanceToInventory(mElementDB.getSubstance("Carbon"));
		addSubstanceToInventory(mElementDB.getSubstance("Oxygen"));	
		addSubstanceToInventory(mElementDB.getSubstance("Nitrogen"));
		addSubstanceToInventory(mElementDB.getSubstance("Sodium"));
		addSubstanceToInventory(mElementDB.getSubstance("Chlorine"));
		//addSubstanceToInventory("Salt");
		//addSubstanceToInventory("Caffeine");
		//addSubstanceToInventory("Water");
		this.setActiveSubstance(mElementDB.getSubstance("Hydrogen"));
		
		
		
	}
	
	public boolean containsSymbol(final String m)
	{
		for(InventoryObject s : mInventoryObjects)
		{
			if(s.mSubstance.mSymbol.equals(m))
			{
				return true;
			}
		}
		return false;
	}
	
	
	public void clearMixingList()
	{
		mMixingList.clear();	
	}
	
	
	public void finalizeMixingList()
	{
		Substance molecule = mElementDB.combine(mMixingList);
		if(molecule != null && !containsSymbol(molecule.mSymbol))
		{
			addSubstanceToInventory(molecule);
		}
		
		clearMixingList();
	}
	
	public void addSubstanceToMixingList(final Substance pSubstanceName)
	{
		
		mMixingList.add(pSubstanceName);
	}
	
	// just make a spot for it.
	public void addSubstanceToInventory(final Substance pSubstance)
	{
		// create new InventoryObject with substance
		int id = mInventoryObjects.size();
		//int pY = 64*(int)(id/4) + 10 + 32*(int)(id/4);
		//int pX = 64 * (id%4) + 22 + 22*(id%4);
		
		int pY = 64*(int)(id/5) + 10 + 32*(int)(id/5);
		int pX = 64 * (id%5) + 10 + 10*(id%5);
		
		System.err.println("id: " + id + ": " + pX + ", " + pY);

		
		mInventoryObjects.add(new InventoryObject(pX, pY, mResourceHandler.getFont("Inventory"), mResourceHandler.getRegion("Flask"), pSubstance));
	}
	
	ArrayList<InventoryObject> getInventoryObjects()
	{
		return mInventoryObjects;
		
	}

	
	/* The "Active Substance" is the thing we're currently throwing around in a flask. */
	// set it
	public void setActiveSubstance(Substance pS)
	{
		mActiveSubstance = pS;
	}
	
	// get it
	public Substance getActiveSubstance() 
	{
		return mActiveSubstance;
	}
	
}
